package com.handydata.antifraud.web;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.handydata.antifraud.bo.alarmruledef.AlarmRuleDefReqBO;
import com.handydata.antifraud.bo.alarmruledef.AlarmRuleDefResBO;
import com.handydata.antifraud.bo.dictinfo.DictInfoReqBO;
import com.handydata.antifraud.bo.dictinfo.DictInfoResBO;
import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.common.exception.AlarmRuleDefExCode;
import com.handydata.antifraud.service.AlarmRuleDefService;
import com.handydata.antifraud.service.DictInfoService;
import com.handydata.antifraud.vo.alarmruledef.AlarmRuleDefBeanVO;
import com.handydata.antifraud.vo.alarmruledef.CreateAlarmRuleDefReqVO;
import com.handydata.antifraud.vo.alarmruledef.CreateAlarmRuleDefResVO;
import com.handydata.antifraud.vo.alarmruledef.DeleteAlarmRuleDefReqVO;
import com.handydata.antifraud.vo.alarmruledef.DeleteAlarmRuleDefResVO;
import com.handydata.antifraud.vo.alarmruledef.DictAlarmRuleDefResVO;
import com.handydata.antifraud.vo.alarmruledef.ModifyAlarmRuleDefReqVO;
import com.handydata.antifraud.vo.alarmruledef.ModifyAlarmRuleDefResVO;
import com.handydata.antifraud.vo.alarmruledef.QueryAlarmRuleDefReqVO;
import com.handydata.antifraud.vo.alarmruledef.QueryAlarmRuleDefResVO;
import com.handydata.antifraud.vo.dictinfo.DictInfoBeanVO;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.web.AbsController;

@RequestMapping("/earlyWarningManage")
@RestController
public class AlarmRuleDefController extends AbsController {
	
	private static final Logger logger = LoggerFactory.getLogger(AlarmRuleDefController.class);
	
	@Autowired
	private AlarmRuleDefService alarmRuleDefService;

	@Autowired
	private DictInfoService dictInfoService;
	
	@ApiOperation(value="新增报警规则定义表", notes="新增报警规则定义表")
	@ApiImplicitParam(name = "req", value = "新增报警规则定义表请求对象", required = true, dataType = "CreateAlarmRuleDefReqVO", paramType = "body")
	@PostMapping("/addEarlySetInfo")
	public CreateAlarmRuleDefResVO createAlarmRuleDef(@RequestBody CreateAlarmRuleDefReqVO req) {
		try {
			logger.info("Execute createAlarmRuleDef, param is {}", req);
			AlarmRuleDefReqBO reqBO = CommonBeanConvertor.convert(req, AlarmRuleDefReqBO.class);
			AlarmRuleDefResBO resBO = alarmRuleDefService.createAlarmRuleDef(reqBO);
			CreateAlarmRuleDefResVO res = CommonBeanConvertor.convert(resBO, CreateAlarmRuleDefResVO.class);
			return res;
		} catch (Exception e) {
			return handleException(e, CreateAlarmRuleDefResVO.class, req, AlarmRuleDefExCode.SYS_UNKNOWN_ERROR);
		}
	}

	@ApiOperation(value="修改报警规则定义表", notes="修改报警规则定义表")
	@ApiImplicitParam(name = "req", value = "修改报警规则定义表请求对象", required = true, dataType = "ModifyAlarmRuleDefReqVO", paramType = "body")
	@PostMapping("/modifyEarlySetInfo")
	public ModifyAlarmRuleDefResVO modifyAlarmRuleDef(@RequestBody ModifyAlarmRuleDefReqVO req) {
		try {
			logger.info("Execute modifyAlarmRuleDef, param is {}", req);
			AlarmRuleDefReqBO reqBO = CommonBeanConvertor.convert(req, AlarmRuleDefReqBO.class);
			AlarmRuleDefResBO resBO = alarmRuleDefService.modifyAlarmRuleDef(reqBO);
			ModifyAlarmRuleDefResVO res = CommonBeanConvertor.convert(resBO, ModifyAlarmRuleDefResVO.class);
			return res;
		} catch (Exception e) {
			return handleException(e, ModifyAlarmRuleDefResVO.class, req, AlarmRuleDefExCode.SYS_UNKNOWN_ERROR);
		}
	}

	@ApiOperation(value="删除报警规则定义表", notes="删除报警规则定义表")
	@ApiImplicitParam(name = "req", value = "修改报警规则定义表请求对象", required = true, dataType = "DeleteAlarmRuleDefReqVO", paramType = "body")
	@PostMapping("/removeEarlySetInfo")
	public DeleteAlarmRuleDefResVO deleteAlarmRuleDef(@RequestBody DeleteAlarmRuleDefReqVO req) {
		try {
			logger.info("Execute deleteAlarmRuleDef, param is {}", req);
			AlarmRuleDefReqBO reqBO = CommonBeanConvertor.convert(req, AlarmRuleDefReqBO.class);
			AlarmRuleDefResBO resBO = alarmRuleDefService.deleteAlarmRuleDef(reqBO);
			DeleteAlarmRuleDefResVO res = CommonBeanConvertor.convert(resBO, DeleteAlarmRuleDefResVO.class);
			return res;
		} catch (Exception e) {
			return handleException(e, DeleteAlarmRuleDefResVO.class, req, AlarmRuleDefExCode.SYS_UNKNOWN_ERROR);
		}
	}

	@ApiOperation(value="查询报警规则定义表", notes="查询报警规则定义表")
	@ApiImplicitParam(name = "req", value = "查询报警规则定义表请求对象", required = true, dataType = "QueryAlarmRuleDefReqVO", paramType = "body")
	@PostMapping("/earlyWarnSetInfoDetail")
	public QueryAlarmRuleDefResVO queryAlarmRuleDefDetail(@RequestBody QueryAlarmRuleDefReqVO req) {
		try {
			logger.info("Execute queryAlarmRuleDef, param is {}", req);
			AlarmRuleDefReqBO reqBO = CommonBeanConvertor.convert(req, AlarmRuleDefReqBO.class);
			AlarmRuleDefResBO resBO = alarmRuleDefService.queryAlarmRuleDef(reqBO);

			QueryAlarmRuleDefResVO res = CommonBeanConvertor.convert(resBO, QueryAlarmRuleDefResVO.class, AlarmRuleDefBeanVO.class);
			//if(res.)//翻译一些代码
			return res;
		} catch (Exception e) {
			return handleException(e, QueryAlarmRuleDefResVO.class, req, AlarmRuleDefExCode.SYS_UNKNOWN_ERROR);
		}
	}
	@ApiOperation(value="查询报警规则定义表", notes="查询报警规则定义表")
	@ApiImplicitParam(name = "req", value = "查询报警规则定义表请求对象", required = true, dataType = "QueryAlarmRuleDefReqVO", paramType = "body")
	@PostMapping("/earlyWarnManageListQuery")
	public QueryAlarmRuleDefResVO queryAlarmRuleDef(@RequestBody QueryAlarmRuleDefReqVO req) {
		try {
			logger.info("Execute queryAlarmRuleDef, param is {}", req);
			AlarmRuleDefReqBO reqBO = CommonBeanConvertor.convert(req, AlarmRuleDefReqBO.class);
			AlarmRuleDefResBO resBO = alarmRuleDefService.queryAlarmRuleDef(reqBO);

			QueryAlarmRuleDefResVO res = CommonBeanConvertor.convert(resBO, QueryAlarmRuleDefResVO.class, AlarmRuleDefBeanVO.class);
			//if(res.)//翻译一些代码
			return res;
		} catch (Exception e) {
			return handleException(e, QueryAlarmRuleDefResVO.class, req, AlarmRuleDefExCode.SYS_UNKNOWN_ERROR);
		}
	}
	@ApiOperation(value="规则配置产品数据源字典", notes="规则配置产品数据源字典")
	@ApiImplicitParam(name = "req", value = "规则配置产品数据源字典", required = true)
	@PostMapping("/addEarlySetInit")
	public DictAlarmRuleDefResVO addEarlySetInit() {
		try {
			logger.info("Execute queryAlarmRuleDef, param none");
			DictInfoReqBO productReqBo=new DictInfoReqBO();
			productReqBo.setType(DictInfoCode.DICT_PRODUCT.getCode());
			DictInfoResBO productResBO = dictInfoService.queryDictInfo(productReqBo);

			DictInfoReqBO wsReqBo=new DictInfoReqBO();
			wsReqBo.setType(DictInfoCode.DICT_WS.getCode());
			DictInfoResBO wsResBO = dictInfoService.queryDictInfo(wsReqBo);

			DictAlarmRuleDefResVO dictAlarmRuleDefResVO=new DictAlarmRuleDefResVO();


			List<DictInfoBeanVO> productVos = productResBO.getResultList().stream().map((x) -> {
				DictInfoBeanVO r = CommonBeanConvertor.convert(x, DictInfoBeanVO.class);
				r.setLabel(r.getCode());
				r.setValue(r.getName());
				return r;
			}).collect(Collectors.toList());
			dictAlarmRuleDefResVO.setProductNameList(productVos);


			List<DictInfoBeanVO> wsVos = wsResBO.getResultList().stream().map((x) -> {
				DictInfoBeanVO r = CommonBeanConvertor.convert(x, DictInfoBeanVO.class);
				r.setLabel(r.getCode());
				r.setValue(r.getName());
				return r;
			}).collect(Collectors.toList());
			dictAlarmRuleDefResVO.setDataOriginList(wsVos);
			dictAlarmRuleDefResVO.setRespCode(AlarmRuleDefExCode.SUCCESS.getErrorCode());
			dictAlarmRuleDefResVO.setRespMsg(AlarmRuleDefExCode.SUCCESS.getErrorMsg());
			return dictAlarmRuleDefResVO;

		} catch (Exception e) {
			return handleException(e, DictAlarmRuleDefResVO.class,null, AlarmRuleDefExCode.SYS_UNKNOWN_ERROR);
		}
	}
	
}
